import wx
from configobj import ConfigObj

# 设置串口参数的对话框类


class UI_Send_Setting(wx.Dialog):

    def __init__(self, *args, **kwds):

        # 先处理输入参数，然后父类初始化
        if 'sendSetting' in kwds:
            print('sendSetting transport success ')
            self.sendSetting = kwds.pop('sendSetting')

        print(len(self.sendSetting))

        for i in range(len(self.sendSetting)) :
            print ("para"+str(i))
        # 参数调入后初始化
        super(UI_Send_Setting, self).__init__(*args, **kwds)

        self.InitUI()
        self.SetSize((400, 400))
        self.SetTitle("发送参数设置")

    def InitUI(self):

        self.panel = wx.Panel(self)
        self.vbox = wx.BoxSizer(wx.VERTICAL)

        self.sb = wx.StaticBox(self.panel, label='配置发送参数')
        self.sbs = wx.StaticBoxSizer(self.sb, orient=wx.VERTICAL)

        print("ui send setting %s" %self.sendSetting)
        self.hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        self.staticText1 = wx.StaticText(self.panel, label='发送一' )
        self.textCtrl1   = wx.TextCtrl(self.panel, value=" ")
        if len(self.sendSetting) >= 1 :
            self.textCtrl1.SetValue(self.sendSetting['1'])
        self.hbox1.Add(self.staticText1)
        self.hbox1.Add(self.textCtrl1,flag=wx.LEFT, border=15)
        self.sbs.Add(self.hbox1)

        self.hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        self.staticText2 = wx.StaticText(self.panel, label='发送二' )
        self.textCtrl2   = wx.TextCtrl(self.panel, value="222")
        if len(self.sendSetting) >= 2 :
            self.textCtrl2.SetValue(self.sendSetting['2'])
        self.hbox2.Add(self.staticText2)
        self.hbox2.Add(self.textCtrl2,flag=wx.LEFT, border=15)
        self.sbs.Add(self.hbox2)

        self.hbox3 = wx.BoxSizer(wx.HORIZONTAL)
        self.staticText3 = wx.StaticText(self.panel, label='发送三' )
        self.textCtrl3  = wx.TextCtrl(self.panel, value="333")
        if len(self.sendSetting) >= 3 :
            self.textCtrl3.SetValue(self.sendSetting['3'])
        self.hbox3.Add(self.staticText3)
        self.hbox3.Add(self.textCtrl3,flag=wx.LEFT, border=15)
        self.sbs.Add(self.hbox3)

        self.hbox4 = wx.BoxSizer(wx.HORIZONTAL)
        self.staticText4 = wx.StaticText(self.panel, label='发送三' )
        self.textCtrl4  = wx.TextCtrl(self.panel, value="444")
        if len(self.sendSetting) >= 4 :
            self.textCtrl4.SetValue( self.sendSetting['4'] )
        self.hbox4.Add(self.staticText4)
        self.hbox4.Add(self.textCtrl4,flag=wx.LEFT, border=15)
        self.sbs.Add(self.hbox4)

        self.hbox5 = wx.BoxSizer(wx.HORIZONTAL)
        self.staticText5 = wx.StaticText(self.panel, label='发送五')
        self.textCtrl5 = wx.TextCtrl(self.panel, value="555")
        if len(self.sendSetting) >= 5 :
            self.textCtrl5.SetValue(self.sendSetting['5'])
        self.hbox5.Add(self.staticText5)
        self.hbox5.Add(self.textCtrl5, flag=wx.LEFT, border=15)
        self.sbs.Add(self.hbox5)

        self.hbox6 = wx.BoxSizer(wx.HORIZONTAL)
        self.staticText6 = wx.StaticText(self.panel, label='发送六' )
        self.textCtrl6   = wx.TextCtrl(self.panel, value=" ")
        if len(self.sendSetting) >= 6 :
            self.textCtrl6.SetValue(self.sendSetting['6'])
        self.hbox6.Add(self.staticText6)
        self.hbox6.Add(self.textCtrl6,flag=wx.LEFT, border=15)
        self.sbs.Add(self.hbox6)

        self.hbox7 = wx.BoxSizer(wx.HORIZONTAL)
        self.staticText7 = wx.StaticText(self.panel, label='发送七' )
        self.textCtrl7   = wx.TextCtrl(self.panel, value=" ")
        if len(self.sendSetting) >= 7 :
            self.textCtrl7.SetValue(self.sendSetting['7'])
        self.hbox7.Add(self.staticText7)
        self.hbox7.Add(self.textCtrl7,flag=wx.LEFT, border=15)
        self.sbs.Add(self.hbox7)

        self.hbox8 = wx.BoxSizer(wx.HORIZONTAL)
        self.staticText8 = wx.StaticText(self.panel, label='发送八' )
        self.textCtrl8   = wx.TextCtrl(self.panel, value=" ")
        if len(self.sendSetting) >= 8 :
            self.textCtrl8.SetValue(self.sendSetting['8'])
        self.hbox8.Add(self.staticText8)
        self.hbox8.Add(self.textCtrl8,flag=wx.LEFT, border=15)
        self.sbs.Add(self.hbox8)

        self.panel.SetSizer(self.sbs)

        self.hbox_0 = wx.BoxSizer(wx.HORIZONTAL)
        self.okButton = wx.Button(self, label=u'保存配置')
        self.closeButton = wx.Button(self, label='Close')
        self.hbox_0.Add(self.okButton)
        self.hbox_0.Add(self.closeButton, flag=wx.LEFT, border=5)

        self.vbox.Add(self.panel, proportion=1,
                flag=wx.ALL | wx.EXPAND, border=5)
        self.vbox.Add(self.hbox_0, flag=wx.ALIGN_CENTER | wx.TOP | wx.BOTTOM, border=10)

        self.SetSizer(self.vbox)

        self.okButton.Bind(wx.EVT_BUTTON, self.OnSave)
        self.closeButton.Bind(wx.EVT_BUTTON, self.OnClose)
 
    # def LoadData(self,e):

    #     pass

    def saveData(self, e):

        pass   

    def OnClose(self, e):

        self.Destroy()

    def OnSave(self, e):
        success = True
        # 赋值字典，写入文件   
        # self.sendSetting['1'] = self.textCtrl1.GetValue( )
        # self.sendSetting['2'] = self.textCtrl1.GetValue( )
        # self.sendSetting['3'] = self.textCtrl1.GetValue( )
        # 写入失败 赋值success false
  
        conf_ini = "config.ini"
        config = ConfigObj(conf_ini)
        config['SendSetting']['1'] = self.textCtrl1.GetValue( )
        config['SendSetting']['2'] = self.textCtrl2.GetValue( )
        config['SendSetting']['3'] = self.textCtrl3.GetValue( )
        config['SendSetting']['4'] = self.textCtrl4.GetValue( )
        config['SendSetting']['5'] = self.textCtrl5.GetValue( )
        config['SendSetting']['6'] = self.textCtrl6.GetValue( )
        config['SendSetting']['7'] = self.textCtrl7.GetValue( )
        config['SendSetting']['8'] = self.textCtrl8.GetValue( )

        config.write()
        # 写入失败 赋值success false
        if success:
            self.EndModal(wx.ID_OK)
        if success:
            self.EndModal(wx.ID_OK)


